{% extends "custom_admin/base.html" %}

{% block title %}部门管理{% endblock %}

{% block content %}
<div class="page-heading">
    <h4><i class="fas fa-building me-2"></i>部门管理</h4>
</div>

<div class="card mb-4">
    <div class="card-body">
        <div class="d-flex justify-content-between flex-wrap mb-3">
            <div class="mb-2">
                <a href="{% url 'custom_admin:department_create' %}" class="btn btn-primary btn-sm">
                    <i class="fas fa-plus me-1"></i>创建部门
                </a>
            </div>
        </div>

        <div class="table-responsive">
            <table class="table table-striped">
                <thead>
                    <tr>
                        <th>部门名称</th>
                        <th>部门编码</th>
                        <th>描述</th>
                        <th>状态</th>
                        <th>操作</th>
                    </tr>
                </thead>
                <tbody>
                    {% for department in departments %}
                    <tr>
                        <td>
                            {% if department.get_children %}
                                <button class="btn btn-sm btn-link p-0" type="button" data-bs-toggle="collapse" 
                                        data-bs-target="#children-{{ department.id }}" aria-expanded="false">
                                    <i class="fas fa-chevron-down"></i>
                                </button>
                            {% endif %}
                            {{ department.name }}
                        </td>
                        <td>{{ department.code }}</td>
                        <td>{{ department.description|truncatewords:10 }}</td>
                        <td>
                            {% if department.is_active %}
                            <span class="badge bg-success">启用</span>
                            {% else %}
                            <span class="badge bg-secondary">禁用</span>
                            {% endif %}
                        </td>
                        <td>
                            <div class="btn-group" role="group">
                                <a href="{% url 'custom_admin:department_create_sub' department.pk %}" class="btn btn-sm btn-outline-primary">
                                    <i class="fas fa-sitemap"></i> 创建子部门
                                </a>
                                <a href="{% url 'custom_admin:department_edit' department.pk %}" class="btn btn-sm btn-outline-primary">
                                    <i class="fas fa-edit"></i> 编辑
                                </a>
                                <a href="{% url 'custom_admin:department_delete' department.pk %}" class="btn btn-sm btn-outline-danger">
                                    <i class="fas fa-trash"></i> 删除
                                </a>
                            </div>
                        </td>
                    </tr>
                    {% if department.get_children %}
                    <tr>
                        <td colspan="5" class="p-0">
                            <div class="collapse" id="children-{{ department.id }}">
                                <table class="table table-sm mb-0">
                                    <tbody>
                                        {% for child in department.get_children %}
                                        <tr>
                                            <td class="ps-5">{{ child.name }}</td>
                                            <td>{{ child.code }}</td>
                                            <td>{{ child.description|truncatewords:10 }}</td>
                                            <td>
                                                {% if child.is_active %}
                                                <span class="badge bg-success">启用</span>
                                                {% else %}
                                                <span class="badge bg-secondary">禁用</span>
                                                {% endif %}
                                            </td>
                                            <td>
                                                <div class="btn-group" role="group">
                                                    <a href="{% url 'custom_admin:department_edit' child.pk %}" class="btn btn-sm btn-outline-primary">
                                                        <i class="fas fa-edit"></i> 编辑
                                                    </a>
                                                    <a href="{% url 'custom_admin:department_delete' child.pk %}" class="btn btn-sm btn-outline-danger">
                                                        <i class="fas fa-trash"></i> 删除
                                                    </a>
                                                </div>
                                            </td>
                                        </tr>
                                        {% endfor %}
                                    </tbody>
                                </table>
                            </div>
                        </td>
                    </tr>
                    {% endif %}
                    {% empty %}
                    <tr>
                        <td colspan="5" class="text-center">暂无部门</td>
                    </tr>
                    {% endfor %}
                </tbody>
            </table>
        </div>
    </div>
</div>
{% endblock %}